import java.math.BigInteger;
import java.util.ArrayList;

public class Worker extends Thread {
	
	public ArrayList<String> data = new ArrayList<String>(); 
	
	public Worker(ArrayList<String> data){
		this.data = data;
	}
	
	@Override
	public void run(){
		try{
			
			for (String string : data){
				
				String[] split = string.split(" ");
				if ((split.length > 1) && (!split[0].equalsIgnoreCase(""))){
					throw new NumberFormatException("For input string: \""+string+"\". Wrong usage. One string - one number ");
				}
				
				if (string.contains(" ")){
					throw new NumberFormatException("For input string: \""+string+"\". No spaces ");
				}
				
				int number = Integer.parseInt(string);
				if (number < 0) {
					throw new NumberFormatException("For input string: \""+string+"\" ");
				}
				
				BigInteger factorial = BigInteger.valueOf(1);
				BigInteger factorialCopy;
				
                for (int i = 1; i <= number; i++){
                	factorialCopy = factorial;
                	factorial = factorial.multiply(BigInteger.valueOf(i));
                	
                    if (factorial.compareTo(factorialCopy) == -1){
                    	throw new NumberFormatException("Too big result of "+number+"! ");
                    }
                }
                
                System.out.println(number+"! = "+factorial);
			}
			
		} catch(NumberFormatException e){
			System.out.print(e.toString());
			System.out.println(". Please, enter positive integer.");
		}
	}
}
